//
//  LGActionSheet.m
//  Gunz
//
//  Created by Joggy on 16/1/19.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "LGActionSheet.h"

@implementation LGActionSheet

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    [self prepareForLayout];
    return self;
}

-  (void)prepareForLayout {
    //灰色背景
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [self frame].size.width, [self frame].size.height)];
    _bgView.backgroundColor = [UIColor blackColor];
    _bgView.alpha = 0.7;
    [self addSubview:_bgView];
    //用手机注册
    _registerByPhoneButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_registerByPhoneButton setBackgroundColor:[UIColor whiteColor]];
    _registerByPhoneButton.layer.cornerRadius = asCornerRadius;
    _registerByPhoneButton.frame = CGRectMake(0, 0, buttonWidth, buttonHeight);
    _registerByPhoneButton.layer.position = CGPointMake([self frame].size.width/2, insert + buttonHeight/2);
    NSMutableAttributedString *phoneTitle = [[NSMutableAttributedString alloc] initWithString:@"手机号注册"];
    NSRange phoneRange = {0, phoneTitle.length};
    [phoneTitle addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:77/255 green:81/255 blue:87/255 alpha:1] range:phoneRange];
    [phoneTitle addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font14*proportion] range:phoneRange];
    [_registerByPhoneButton setAttributedTitle:phoneTitle forState:UIControlStateNormal];
    [self addSubview:_registerByPhoneButton];
    //用邮箱注册
    _registerByEmailButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_registerByEmailButton setBackgroundColor:[UIColor whiteColor]];
    _registerByEmailButton.layer.cornerRadius = asCornerRadius;
    _registerByEmailButton.frame = CGRectMake(0, 0, buttonWidth, buttonHeight);
    _registerByEmailButton.layer.position = CGPointMake([self frame].size.width/2, (insert)*2 + buttonHeight*1.5);
    NSMutableAttributedString *emailTitle = [[NSMutableAttributedString alloc] initWithString:@"邮箱注册"];
    NSRange emailRange = {0, emailTitle.length};
    [emailTitle addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:77/255 green:81/255 blue:87/255 alpha:1] range:emailRange];
    [emailTitle addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font14*proportion] range:emailRange];
    [_registerByEmailButton setAttributedTitle:emailTitle forState:UIControlStateNormal];
    [self addSubview:_registerByEmailButton];
    //取消
    _cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_cancelButton setBackgroundColor:[UIColor grayColor]];
    _cancelButton.layer.cornerRadius = asCornerRadius;
    _cancelButton.frame = CGRectMake(0, 0, buttonWidth, buttonHeight);
    _cancelButton.layer.position = CGPointMake([self frame].size.width/2, (insert)*3 + buttonHeight*2.5);
    NSMutableAttributedString *cancelTitle = [[NSMutableAttributedString alloc] initWithString:@"取消"];
    NSRange cancelRange = {0, cancelTitle.length};
    [cancelTitle addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:cancelRange];
    [cancelTitle addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font14*proportion] range:cancelRange];
    [_cancelButton setAttributedTitle:cancelTitle forState:UIControlStateNormal];
    [self addSubview:_cancelButton];
}

- (void)show {
    _shadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
    _shadowView.backgroundColor = [UIColor clearColor];
    UITapGestureRecognizer *dismissTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [_shadowView addGestureRecognizer:dismissTap];
    _kWindow = [UIApplication sharedApplication].keyWindow;
    [_kWindow addSubview:_shadowView];
    [_kWindow addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        CGRect tempFrame = self.frame;
        tempFrame.origin.y = screenHeight - bgHeight;
        self.frame = tempFrame;
    }];
}

- (void)dismiss {
    if(_shadowView != nil) {
        [_shadowView removeFromSuperview];
    }
    [UIView animateWithDuration:0.3 animations:^{
        CGRect tempFrame = self.frame;
        tempFrame.origin.y = screenHeight;
        self.frame = tempFrame;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
